<?php

/*
 * sfWidgetFormDateForMobile represents a date widget adapted to render properly
 * in mobile devices.
 *
 * @author     Jacobo Martínez <jacobo.martinez@equilibrio.net.ve>
 * @version    1.0
 */

class sfWidgetFormDateForMobile extends sfWidgetForm
{
    /**
    * Configures the current widget.
    *
    * Available options:
    *
    *  * format:       The date format string (%day%/%month%/%year% by default)
    *  * can_be_empty: Whether the widget accept an empty value (true by default)
    *  * empty_values: An array of values to use for the empty value (empty string for year, month, and day by default)
    *
    * @param array $options     An array of options
    * @param array $attributes  An array of default HTML attributes
    *
    * @see sfWidgetForm
    */
    protected function configure($options = array(), $attributes = array())
    {
        $this->addOption('format', '%day%/%month%/%year%');
        $this->addOption('can_be_empty', true);
        $this->addOption('empty_values', array('year' => '', 'month' => '', 'day' => ''));
    }

    /**
    * @param  string $name        The element name
    * @param  string $value       The date displayed in this widget
    * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
    * @param  array  $errors      An array of errors for the field
    *
    * @return string An HTML tag string
    *
    * @see sfWidgetForm
    */
    public function render($name, $value = null, $attributes = array(), $errors = array())
    {
        // convert value to an array
        $default = array('year' => null, 'month' => null, 'day' => null);
        if (is_array($value))
        {
            $value = array_merge($default, $value);
        }
        else
        {
            $value = (string) $value == (string) (integer) $value ? (integer) $value : strtotime($value);
            if (false === $value)
            {
                $value = $default;
            }
            else
            {
                $value = array('year' => date('Y', $value), 'month' => date('n', $value), 'day' => date('j', $value));
            }
        }

        $date = array();
        $emptyValues = $this->getOption('empty_values');
        
        $date['%day%'] = $this->renderDayWidget($name.'[day]',
                $value['day'],
                array('id_format' => $this->getOption('id_format')),
                array_merge($this->attributes, $attributes));
        $date['%month%'] = $this->renderMonthWidget($name.'[month]',
                $value['month'],
                array('id_format' => $this->getOption('id_format')),
                array_merge($this->attributes, $attributes));
        $date['%year%'] = $this->renderYearWidget($name.'[year]',
                $value['year'],
                array('id_format' => $this->getOption('id_format')),
                array_merge($this->attributes, $attributes));

        return strtr($this->getOption('format'), $date);
    }

    /**
    * @param string $name
    * @param string $value
    * @param array $options
    * @param array $attributes
    * @return string rendered widget
    */
    protected function renderDayWidget($name, $value, $options, $attributes)
    {
        $attributes['maxlength'] = 2;
        $attributes['size'] = 2;

        $widget = new sfWidgetFormInputText($options, $attributes);
        return $widget->render($name, $value);
    }

    /**
    * @param string $name
    * @param string $value
    * @param array $options
    * @param array $attributes
    * @return string rendered widget
    */
    protected function renderMonthWidget($name, $value, $options, $attributes)
    {
        $attributes['maxlength'] = 2;
        $attributes['size'] = 2;

        $widget = new sfWidgetFormInputText($options, $attributes);
        return $widget->render($name, $value);
    }

    /**
    * @param string $name
    * @param string $value
    * @param array $options
    * @param array $attributes
    * @return string rendered widget
    */
    protected function renderYearWidget($name, $value, $options, $attributes)
    {
        $attributes['maxlength'] = 4;
        $attributes['size'] = 4;

        $widget = new sfWidgetFormInputText($options, $attributes);
        return $widget->render($name, $value);
    }
}